/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import persistence.entites.Tag;
import javax.annotation.ManagedBean;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean
@SessionScoped
public class TagController extends Controller {

    private Tag tag = null;
    private transient DataModel tags = null;

    public TagController() {
        super();
        refreshTags();
    }

    public void refreshTags() {
        tags = new ListDataModel();
        tags.setWrappedData(connMySQL.getTagDAO().findAll());
    }

    public String index() {
        refreshTags();
        return "taglist";
    }

    public String ajouterDetail() {
        tag = new Tag();
        return "tagdetail";
    }

    public String add() {
        if (connMySQL.getTagDAO().findById(tag.getId()) != null) {
            return "tagdetail";
        } else {
            connMySQL.getTagDAO().insert(tag);
            return index();
        }
    }

    public String delete() {
        tag = (Tag) tags.getRowData();
        connMySQL.getTagDAO().delete(tag);
        return index();
    }

    /**********************
     ***** Accesseurs *****
     **********************/
 
    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public DataModel getTags() {
        return tags;
    }

    public void setTags(DataModel tags) {
        this.tags = tags;
    }
}
